﻿// <kernel for logging mobile worker.>
//     Copyright (C) 2013  APKO
// 
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace kernel.gui.controllers {
    internal class NumberTextBox : TextBox {
        static NumberTextBox() {
            EventManager.RegisterClassHandler(
                typeof (NumberTextBox),
                DataObject.PastingEvent,
                (DataObjectPastingEventHandler) ((sender, e) => {
                                                     if (IsDataValid(e.DataObject)) {
                                                         return;
                                                     }
                                                     var data = new DataObject();
                                                     data.SetText(String.Empty);
                                                     e.DataObject = data;
                                                     e.Handled = false;
                                                 }));
        }

        protected override void OnDrop(DragEventArgs e) {
            e.Handled = !IsDataValid(e.Data);
            base.OnDrop(e);
        }

        protected override void OnDragOver(DragEventArgs e) {
            if (!IsDataValid(e.Data)) {
                e.Handled = true;
                e.Effects = DragDropEffects.None;
            }
            base.OnDragEnter(e);
        }

        private static Boolean IsDataValid(IDataObject data) {
            Boolean isValid = false;
            if (data != null) {
                var text = data.GetData(DataFormats.Text) as String;
                if (!String.IsNullOrEmpty(text == null ? null : text.Trim())) {
                    Int32 result;
                    if (Int32.TryParse(text, out result)) {
                        if (result > 0) {
                            isValid = true;
                        }
                    }
                }
            }
            return isValid;
        }

        protected override void OnKeyDown(KeyEventArgs e) {
            if (e.Key >= Key.D0 && e.Key <= Key.D9) {
                return;
            }
            if (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) {
                return;
            }
            if (e.Key != Key.Back) {
                e.Handled = true;
            }
        }
    }
}